package com.power.using.mydaggerp.presenter.home.tabs;

import android.os.Bundle;

import com.orhanobut.logger.Logger;
import com.power.using.mydaggerp.constant.BundleKeyConstant;
import com.power.using.mydaggerp.contract.home.tabs.ZhihuContract;
import com.power.using.mydaggerp.model.bean.zhihu.ZhihuDailyItemBean;
import com.power.using.mydaggerp.model.bean.zhihu.ZhihuDailyListBean;
import com.power.using.mydaggerp.model.home.tabs.ZhihuModel;
import com.power.using.mydaggerp.ui.activity.detial.ZhihuDailyDetailActivity;
import com.zyw.horrarndoo.sdk.utils.ToastUtils;

import io.reactivex.functions.Consumer;

/**
 * Created by 聪明一只哈 on 2019/10/11.
 * Time: 2019/10/11  11:28
 */
public class ZhihuPresenter extends ZhihuContract.ZhihuPresenter{


    private String mDate;

    public static ZhihuPresenter newInstance(){
        return new ZhihuPresenter();
    }


    @Override
    public void loadLatestList() {

        if(mIModel==null)
            return;

        mRxManage.register(mIModel.getDailyList().subscribe(new Consumer<ZhihuDailyListBean>() {
            @Override
            public void accept(ZhihuDailyListBean zhihuDailyListBean) throws Exception {
                mDate = zhihuDailyListBean.getDate();

                if (mIView != null) {
                    mIView.updateContentList(zhihuDailyListBean.getStories());
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if(mIView!=null){
                    if(mIView.isVisiable()){
                        mIView.showToast("NetWork error");
                    }
                    mIView.showNetworkError();
                }
            }
        }));

    }

    @Override
    public void loadMoreList() {
        if(mIModel==null)
            return;

        mRxManage.register(mIModel.getDailyList(mDate).subscribe(new Consumer<ZhihuDailyListBean>() {
            @Override
            public void accept(ZhihuDailyListBean zhihuDailyListBean) throws Exception {
                if (mDate.equals(zhihuDailyListBean.getDate()))
                    return;
                mDate = zhihuDailyListBean.getDate();

                if (mIView != null) {
                    mIView.updateContentList(zhihuDailyListBean.getStories());
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if(mIView!=null){
                    mIView.showLoadMoreError();
                }
            }
        }));


    }

    @Override
    public void onItemClick(final int position, ZhihuDailyItemBean item) {

        mRxManage.register(mIModel.recordItemIsRead(item.getId())
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (mIView == null)
                            return;

                        if (aBoolean) {
                            mIView.itemNotifyChanged(position);
                        } else {
                            ToastUtils.showToast("写入点击状态值失败");
                            Logger.e("写入点击状态值失败");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));

        if(mIView==null)
            return;

        Bundle bundle = new Bundle();
        bundle.putString(BundleKeyConstant.ARG_KEY_ZHIHU_DETAIL_ID, item.getId());
        bundle.putString(BundleKeyConstant.ARG_KEY_ZHIHU_DETAIL_TITLE, item.getTitle());
        mIView.startNewActivity(ZhihuDailyDetailActivity.class, bundle);
    }

    @Override
    protected ZhihuContract.IZhihuModel getModel() {
        return ZhihuModel.newInstance();
    }

    @Override
    public void onStart() {

    }
}
